package com.bbarou.supercifs;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import android.util.Log;

public class ShellExecuter {

   public static ShellExecuter.Result Execute(String var0) throws IOException, InterruptedException {
      return ExecuteByInterpreter("sh", Arrays.asList(new String[]{var0}));
   }

   public static ShellExecuter.Result Execute(List var0) throws IOException, InterruptedException {
      return ExecuteByInterpreter("sh", var0);
   }

   public static ShellExecuter.Result ExecuteAsRoot(String var0) throws IOException, InterruptedException {
      return ExecuteByInterpreter("su", Arrays.asList(new String[]{var0}));
   }

   public static ShellExecuter.Result ExecuteAsRoot(List var0) throws IOException, InterruptedException {
      return ExecuteByInterpreter("su", var0);
   }

   public static ShellExecuter.Result ExecuteByInterpreter(String var0, List var1) throws IOException, InterruptedException {
      Process var2 = Runtime.getRuntime().exec(var0);
      DataOutputStream var3 = new DataOutputStream(var2.getOutputStream());
      DataInputStream var4 = new DataInputStream(var2.getInputStream());
      DataInputStream var5 = new DataInputStream(var2.getErrorStream());
      ShellExecuter.ReadThread var6 = new ShellExecuter.ReadThread(var4);
      ShellExecuter.ReadThread var7 = new ShellExecuter.ReadThread(var5);
      var6.start();
      var7.start();
      Iterator var8 = var1.iterator();

      while(var8.hasNext()) {
         var3.writeBytes((String)var8.next() + "\n");
         var3.flush();
      }

      var3.writeBytes("exit\n");
      var3.flush();
      var3.close();
      var6.join();
      var7.join();
      var4.close();
      var5.close();
      Log.v("SUPER8CIFS", "ShellExecuter: "+var6.getOutput());
      Log.v("SUPER8CIFS", "ShellExecuter: "+var7.getOutput());
      return new ShellExecuter.Result(var6.getOutput(), var7.getOutput(), var2.waitFor());
   }

   public static int ProbePath(String var0) {
      int var7;
      if((new File(var0)).exists()) {
         var7 = 0;
      } else {
         String[] var1 = var0.split("/");
         String var2 = "";
         boolean var3 = true;
         ArrayList var4 = new ArrayList();

         for(int var5 = 1; var5 < var1.length; ++var5) {
            var2 = var2 + "/" + var1[var5];
            if(var3) {
               if((new File(var2)).exists()) {
                  continue;
               }

               var3 = false;
            }

            var4.add("mkdir " + getQuotedString(var2));
         }

         if(var4.size() > 0) {
            int var9;
            try {
               var9 = ExecuteAsRoot((List)var4).getExitValue();
            } catch (Exception var10) {
               var10.printStackTrace();
               var7 = -1;
               return var7;
            }

            var7 = var9;
         } else {
            var7 = 0;
         }
      }

      return var7;
   }

   public static String getQuotedString(String var0) {
      return "\'" + var0.replaceAll("\'", "\'\"\'\"\'") + "\'";
   }

   public static class Result implements Serializable {

      private static final long serialVersionUID = 1L;
      private String error;
      private int exitValue;
      private String output;


      public Result() {
         this.error = null;
         this.output = null;
         this.exitValue = 0;
      }

      public Result(String var1, String var2, int var3) {
         this.output = var1;
         this.error = var2;
         this.exitValue = var3;
      }

      public String getError() {
         return this.error;
      }

      public int getExitValue() {
         return this.exitValue;
      }

      public String getOutput() {
         return this.output;
      }

      public boolean wasSuccessful() {
         boolean var1;
         if(this.exitValue == 0) {
            var1 = true;
         } else {
            var1 = false;
         }

         return var1;
      }
   }

   static class ReadThread extends Thread {

      static final int BUF_SIZE = 1024;
      StringBuilder output;
      DataInputStream stream;


      public ReadThread(DataInputStream var1) {
         this.stream = var1;
         this.output = new StringBuilder();
      }

      public String getOutput() {
         return this.output.toString();
      }

      public void run() {
         byte[] var1 = new byte[1024];

         try {
            while(true) {
               int var3 = this.stream.read(var1, 0, 1024);
               if(var3 == -1) {
                  break;
               }

               this.output.append(new String(var1, 0, var3));
            }
         } catch (IOException var4) {
            var4.printStackTrace();
         }

      }
   }
}
